//
//  DestinationViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "DestinationViewController.h"
#import "Bigmodel.h"
#import "DesrictionCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "BannerCollectionReusableView.h"
#import "BannerViewController.h"
#import "GiFHUD.h"
#import "CityViewController.h"
#import "FuJinViewController.h"
#import "ZHONGJIANViewController.h"
@interface DestinationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, BannerCollectionReusableViewDelegate, HeaderCollectionReusableViewDelegate>
@property (nonatomic, retain) BaseCollectionView *collection;/**<  底层collectionView */
@property (nonatomic, retain) NSMutableDictionary *dataSourceDic;/**<  整个界面的数据 */
@property (nonatomic, retain) NSMutableArray *titleArr; /**<  每个分区的标题 */
@property (nonatomic, retain) NSMutableArray *moreArr;/**<  装判断有没有更多的数组 */
@property (nonatomic, retain) NSMutableArray *dataArr;/**<  banner广告数据轮播 */
@property (nonatomic, retain) DCPicScrollView *dc;/**<  轮播图 */

@end

@implementation DestinationViewController

- (void)dealloc{
    [_collection release];
    [_dataArr release];
    [_dataSourceDic release];
    [_titleArr release];
    [_moreArr release];
    [_dc release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
/** 实现协议方法跳转页面 */
- (void)JumeToVc:(NSString *)URL{
    
    BannerViewController *ban = [[BannerViewController alloc] init];
    
    ban.URL = URL;
    
    [self.navigationController pushViewController:ban animated:YES];
    [ban release];
    
    
}

- (void)jumpToMoreVc:(NSString *)url{
    
    ZHONGJIANViewController *zhang = [[ZHONGJIANViewController alloc] init];
    
    zhang.url = url;
    [self.navigationController pushViewController:zhang animated:YES];
    
    [zhang release];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"想去哪";
    
    //数组, 字典 初始化（最好放在这里）
    self.dataSourceDic = [NSMutableDictionary dictionary];
    self.titleArr = [NSMutableArray array];
    self.moreArr = [NSMutableArray array];
    [self handdelData];
    [self CreateCollectionView];
    
    [GiFHUD setGifWithImageName:@"b879f2e742d94b2a8f2362d0ca7e18c7.gif"];
    [GiFHUD show];
    
    
    
    
}
#pragma mark- Collection创建
- (void)CreateCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    
    layout.headerReferenceSize = CGSizeMake(WIDTH, 30);
    
    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    
    [self.view addSubview:_collection];
    [_collection release];
    
    //注册
    [_collection registerClass:[DesrictionCollectionViewCell class] forCellWithReuseIdentifier:@"DesrictionCollectionViewCell"];
    //注册header
    [_collection registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderCollectionReusableView"];
    //注册
    [_collection registerClass:[BannerCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BannerCollectionReusableView"];
    
}


/**<  数据处理 */
- (void)handelDataBanner{
    
    [AFNetWorkingHandle GETRequestWithURLString:@"http://api.breadtrip.com/destination/v3/?last_modified_since=0" completionWithSuccess:^(id result) {
        
        NSArray *arr = result[@"banners"];
        
        self.dataArr = [DesBanner ModelHandleWithArray:arr];
        
        NSLog(@"%@", _dataArr);
        
        
    } withFailure:^(id error) {
        NSLog(@"%@", error);
        
        
    }];
}

#pragma mark delegate方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld %ld", (long)indexPath.section, (long)indexPath.row);
    if (indexPath.section == 1) {
       
        NSArray *arr = _dataSourceDic[@"「春节最佳旅行地」"];
       
        
        Bigmodel *model = arr[indexPath.row];
        
     
        
        FuJinViewController *fujin = [[FuJinViewController alloc] init];
        fujin.name = model.name;
        fujin.location = model.location;
        [self.navigationController pushViewController:fujin animated:YES];
        
        [fujin release];
        
    }
    
    if (indexPath.section == 2) {
        NSArray *arr = _dataSourceDic[@"热门地点"];
        
        Bigmodel *model = arr[indexPath.row];
        FuJinViewController *fujin = [[FuJinViewController alloc] init];
        fujin.name = model.name;
        
        fujin.location = model.location;
        [self.navigationController pushViewController:fujin animated:YES];
        
        [fujin release];
    }
    
    if (indexPath.section == 3) {
        NSArray *arr = _dataSourceDic[@"欧洲国家"];
        
        Bigmodel *model = arr[indexPath.row];
        
        FuJinViewController *fujin = [[FuJinViewController alloc] init];
        fujin.name = model.name;
        fujin.location = model.location;
        [self.navigationController pushViewController:fujin animated:YES];
        
        [fujin release];

    }
    
    if (indexPath.section == 4) {
        NSArray *arr = _dataSourceDic[@"港澳台"];
        
        Bigmodel *model = arr[indexPath.row];
        
        FuJinViewController *fujin = [[FuJinViewController alloc] init];
        fujin.name = model.name;
        fujin.location = model.location;
        [self.navigationController pushViewController:fujin animated:YES];
        
        [fujin release];
    }
    if (indexPath.section == 5) {
        NSArray *arr = _dataSourceDic[@"美洲和大洋洲"];
        
        Bigmodel *model = arr[indexPath.row];
        
        FuJinViewController *fujin = [[FuJinViewController alloc] init];
        fujin.name = model.name;
        fujin.location = model.location;
        [self.navigationController pushViewController:fujin animated:YES];
        
        [fujin release];
    }
    
    if (indexPath.section == 6) {
        NSArray *arr = _dataSourceDic[@"亚洲国家"];
        
        Bigmodel *model = arr[indexPath.row];
        
        FuJinViewController *fujin = [[FuJinViewController alloc] init];
        fujin.name = model.name;
        fujin.location = model.location;
        [self.navigationController pushViewController:fujin animated:YES];
        
        [fujin release];
    }
    
    if (indexPath.section == 7) {
        NSArray *arr = _dataSourceDic[@"国内城市"];
        
        Bigmodel *model = arr[indexPath.row];
        
        FuJinViewController *fujin = [[FuJinViewController alloc] init];
        fujin.name = model.name;
        fujin.location = model.location;
        [self.navigationController pushViewController:fujin animated:YES];
        
        [fujin release];
    }
    
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _titleArr.count + 1;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return CGSizeMake(WIDTH, 200);
    }else{
        return CGSizeMake(WIDTH, 20);
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //获取当前section的标题
    if (section == 0) {
        return 0;
    }else{
        NSString *title = _titleArr[section - 1];
        return [_dataSourceDic[title]count];
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        DesrictionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DesrictionCollectionViewCell" forIndexPath:indexPath];
          return cell;
    }else{
        DesrictionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DesrictionCollectionViewCell" forIndexPath:indexPath];
        NSString *title = _titleArr[indexPath.section - 1];
        Bigmodel *model = _dataSourceDic[title][indexPath.row];
        cell.model = model;
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
   
    if ([kind isEqualToString:@"UICollectionElementKindSectionHeader"]) {
        if (indexPath.section == 0) {
            BannerCollectionReusableView *banner = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"BannerCollectionReusableView" forIndexPath:indexPath];
            banner.delegate = self;
            return banner;
        } else{
            HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderCollectionReusableView" forIndexPath:indexPath];
            header.delegate = self;
            NSString *title = _titleArr[indexPath.section - 1];
             NSNumber *more = _moreArr[indexPath.section - 1];
            
            
            [header setTitle:title more:more];
            
            return header;
       }
    }
    return nil;
}


- (void)handdelData{
    [AFNetWorkingHandle GETRequestWithURLString:@"http://api.breadtrip.com/destination/v3/?last_modified_since=0" completionWithSuccess:^(id result) {
        for (NSDictionary *bigDic in result[@"elements"]) {
            //每个分区的数组, 必须放在这 ，每循环一次初始化一次
            NSMutableArray *arr = [NSMutableArray array];
            [_titleArr addObject:bigDic[@"title"]];
            [_moreArr addObject:bigDic[@"index"]];
            NSLog(@"%@", _titleArr);
            for (NSDictionary *smallDic  in bigDic[@"data"]) {
                Bigmodel *model = [[Bigmodel alloc] init];
                [model setValuesForKeysWithDictionary:smallDic];
                [arr addObject:model];
            }
            //字典的key是每个分区的标题，value是每个分区的数组
            [_dataSourceDic setValue:arr forKey:bigDic[@"title"]];
        }
        
        
        
        [_collection reloadData];
        
        [GiFHUD dismiss];
    } withFailure:^(id error) {
        NSLog(@"%@", error);
        
        [GiFHUD dismiss];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
